#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct TreeNode
{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};

class Solution
{
public:
    int maxDepth(TreeNode* root)
    {
        if (root == nullptr) return 0;

        queue<TreeNode*> q;
        q.push(root);

        int depth = 0;
        while (!q.empty())
        {
            int levelSize = q.size();
            depth++;

            while (levelSize--)
            {
                TreeNode* front = q.front();
                q.pop();

                if (front->left) q.push(front->left);
                if (front->right) q.push(front->right);
            }
        }
        return depth;
    }
};

class Solution
{
public:
    int maxDepth(TreeNode* root)
    {
        if (root == nullptr) return 0;

        int leftDepth = maxDepth(root->left);
        int rightDepth = maxDepth(root->right);

        return max(leftDepth, rightDepth) + 1;
    }
};

class Solution
{
    int ret = 0;

public:
    int maxDepth(TreeNode* root)
    {
        dfs(root, 0);
        return ret;
    }

    void dfs(TreeNode* root, int depth)
    {
        if (root == nullptr) return;

        depth++;
        ret = max(ret, depth);

        dfs(root->left, depth);
        dfs(root->right, depth);
    }
};